package org.test.client.ui.component.combox;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import org.test.client.ui.component.field.HighLightTextField;
import org.test.client.ui.component.field.TxtFieldSE;
import org.test.client.ui.util.UIUtil;
import org.test.client.ui.util.VFlowLayout;

import com.sun.awt.AWTUtilities;

public class ComboxContentContainer extends JWindow implements DocumentListener,FocusListener{
	private ComboxContentContainer thisWindow = this;
	
	public HighLightTextField searchField = null;
	
	public MyCombox combox = null;
	public MyComboxModel model = null;
	public int maxHeight = 220;
	public int searchHeight = 30;
	
	public int preHeight = 0;
	public Shape shape = null;
	JPanel itemPanel = null;
	
	public Map<String ,List> showMap = null;
	//增加frame 是为使txtfield 可以获得焦点
	public JFrame frame = null;
	
	public ComboxContentContainer(MyCombox combox, JFrame frame){
		super(frame);
		this.frame = frame;
		this.combox = combox;
		this.model = combox.model;
		doInit();
	}
	
	public void doInit(){
		this.setLayout(new BorderLayout());
		//取消 always on top
//		this.setAlwaysOnTop(true);
		
		itemPanel = new JPanel(new VFlowLayout(VFlowLayout.CENTER, 1, 1, true, false));
		JScrollPane center = new JScrollPane(itemPanel);
		center.setBorder(new EmptyBorder(1,1,1,1));

		searchField = HighLightTextField.createNormalField();
		TxtFieldSE end = TxtFieldSE.createEndIconWithinBorder("fa-search");
		searchField.end = end;
		searchField.iconMarginLR = searchField.iconMarginLR - 3;
		searchField.needHighLight = false;
		searchField.needRound = false;
		searchField.setPreferredSize(new Dimension(0,searchHeight));
		//添加DocumentListener监听器  
		Document doc = searchField.getDocument();  
        doc.addDocumentListener(this);

        if(combox.needSearch){
        	this.add(searchField,BorderLayout.NORTH);
        }
		this.add(center);
		JPanel bottom = new JPanel();
		bottom.setPreferredSize(new Dimension(0,MyCombox.diameter/2));
		this.add(bottom,BorderLayout.SOUTH);
		
		doCenterInit();
		frame.addComponentListener(new ComponentAdapter(){
			public void componentResized(ComponentEvent e) {
				updateDropDownList();
			}
			public void componentMoved(ComponentEvent e) {
				updateDropDownList();
			}
		});
		
		searchField.addFocusListener(thisWindow);
	}
	
	public void updateDropDownList(){
		if(this.isVisible()){
			Point point = new Point(0,0);
			SwingUtilities.convertPointToScreen(point , combox);
			int sx = point.x;
			int sy =  point.y+ combox.getHeight();
			int sw = combox.getWidth();
			int sh = preHeight;
			setLocation(sx, sy);
			setSize(sw,sh);
		}
	}
	
	public void doCenterInit(){
		if(showMap == null ){
			showMap = model.setFilterString("");
		}
		preHeight = 0;
		
		itemPanel.removeAll();
		if(showMap.size() == 0){
			itemPanel.add(new JLabel("No results match.",SwingConstants.CENTER));
			preHeight = 80;
		}else{
			Set<Entry<String,List>> entrySet = showMap.entrySet();
			
			for(Entry<String,List> tmpe : entrySet ){
				String key = tmpe.getKey().toString();
				if(!key.equals(model.NONE_GROUP)){
					ItemLabel titleLabel = new ItemLabel(key, true);
					itemPanel.add(titleLabel);
					preHeight += titleLabel.defaultH;
				}
				List value = tmpe.getValue();
				for(Object obj : value){
					ItemLabel itemLabel = new ItemLabel(obj.toString(), false);
					itemLabel.combox = combox;
					itemLabel.representObj = obj;
					itemPanel.add(itemLabel);
					preHeight += itemLabel.defaultH;
				}
			}
			if(combox.needSearch){
				preHeight += 20+searchHeight; 
			}else{
				preHeight += 20; 
			}
			if(preHeight > maxHeight){
				preHeight = maxHeight;
			}
		}

		this.setSize(this.getWidth(), preHeight); 
		shape = createShape(this.getWidth(), preHeight);
		AWTUtilities.setWindowShape(this,shape);
	}
	
	@Override
	public void paintComponents(Graphics g) {
		super.paintComponents(g);
	}
	
	public Shape createShape(int x , int y ,int w, int h){
		GeneralPath shape = new GeneralPath();
		int diameter = MyCombox.diameter;
		int radius = MyCombox.diameter/2;
		
		shape.moveTo(x, y);
		shape.lineTo(x, h-radius);
		shape.append(new Arc2D.Double(x,h-diameter,diameter,diameter, 180,90,Arc2D.OPEN), true);
		shape.lineTo(w-radius,h);
		shape.append(new Arc2D.Double(w-diameter,h-diameter,diameter,diameter,270, 90,Arc2D.OPEN), true);
		shape.lineTo(w, y);
		shape.lineTo(x,y);
		shape.closePath();
		return shape;
	}
	public Shape createShape(int w, int h){
		return createShape(0,0,w,h);
	}
	
	
	public void showList(int x ,int y,int w ,int h ){
		if(w == this.getWidth() && h==this.getHeight()){
			this.setLocation(x, y);
			this.setVisible(true);
			return;
		}
		this.setLocation(x, y);
		this.setSize(w, h);
		shape = createShape(w, h);
		AWTUtilities.setWindowShape(this,shape);
		this.setGlassPane(new JComponent(){
			@Override
			protected void paintComponent(Graphics g) {
				Graphics2D g2d = (Graphics2D)g;
				UIUtil.setAntiAliasing(g2d, true);
				shape = createShape(0,-1,this.getWidth()-1, this.getHeight()-1);
				g2d.setColor(MyCombox.borderColor);
				g2d.draw(shape);
			}
		});
		this.getGlassPane().setVisible(true);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		MyComboxModel mm = new MyComboxModel(true);
		mm.addItemByGroup("NFC EAST", "Dallas Gayboys", false);
		mm.addItemByGroup("NFC EAST", "New York Gaints", false);
		mm.addItemByGroup("NFC EAST", "Philadelphia Eagles", false);
		mm.addItemByGroup("NFC EAST", "Washington Redskins", false);
		
		mm.addItemByGroup("NFC NORTH", "Chicago Bears", false);
		mm.addItemByGroup("NFC NORTH", "Detroit Lions", false);
		mm.addItemByGroup("NFC NORTH", "Green Bay Packers", false);
		mm.addItemByGroup("NFC NORTH", "Minnesota Vikings", false);

		mm.addItemByGroup("NFC SOUTH", "Atlanta Falcons", false);
		mm.addItemByGroup("NFC SOUTH", "Carolina Panthers", false);
		mm.addItemByGroup("NFC SOUTH", "New Orleans Saints", false);
		mm.addItemByGroup("NFC SOUTH", "Tampa Bay Buccaneers", false);
		
		mm.addItemByGroup("NFC WEST", "Denver Broncos", false);
		mm.addItemByGroup("NFC WEST", "Kansas City Chiefs", false);
		mm.addItemByGroup("NFC WEST", "Oakland Raiders", false);
		mm.addItemByGroup("NFC WEST", "San Diego Chargers", false);
		
		MyCombox myCombox = new MyCombox(mm,"-- -- ");
		myCombox.needSearch = false;
//		myCombox.needSearch = true;
		
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setSize(500, 400);
		UIUtil.centerWindow(frame);
		
		Container contentPane = frame.getContentPane();
		JPanel ppp = new JPanel(null);
		ppp.add(myCombox);
		ppp.setBackground(Color.white);
		
		myCombox.setBounds(10, 10, 200, 25);
		contentPane.add(ppp);
		contentPane.add(new JButton("AAAA"),BorderLayout.NORTH);
		
		
//		ComboxContentContainer w = new ComboxContentContainer(mm,frame);
//		UIUtil.centerWindow(w);
//		w.setVisible(true);
	}


	@Override
	public void changedUpdate(DocumentEvent e) {
		doAction(e);
	}
	@Override
	public void insertUpdate(DocumentEvent e) {
		doAction(e);
	}
	@Override
	public void removeUpdate(DocumentEvent e) {
		doAction(e);
	}
	public void doAction(DocumentEvent e){
		Document doc = e.getDocument();
		try {
			String s = doc.getText(0, doc.getLength());
//			System.out.println(s);
			showMap = model.setFilterString(s);
			doCenterInit();
			
			thisWindow.doLayout();
			thisWindow.revalidate();
			
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		if(combox.needSearch){
			thisWindow.setVisible(false);
			combox.setSelected(false);
		}
	}

}
